package testgrails

import static org.junit.Assert.*
import org.junit.*

class BidIntegrationTests {

    @Before
    void setUp() {
        // Setup logic here
    }

    @After
    void tearDown() {
        // Tear down logic here
    }

	//B-5: The Bid amount must be at least .50 higher than the previous Bid for the same listing (integration test)
    @Test
    void testSomething() {

	//seller -- required for listing
	def seller = new Customer(emailAddress : 'bushrat9@gmail.com', password: 'password', dateCreated: new Date())
	seller.save(flush:true, failOnError: true)
	assertNotNull (seller.save(flush: true, failOnError: true))
	def sellerFromDB = Customer.findByEmailAddress("bushrat9@gmail.com")
	assertNotNull(sellerFromDB)

	//bids -- required for listing, bidder -- required for bid
	def bidder = new Customer(emailAddress: 'bushrat1@gmail.com', password: '1234567' , dateCreated: new Date())
	assertNotNull (bidder.save(flush: true, failOnError: true))
	def bidderFromDB = Customer.findByEmailAddress("bushrat1@gmail.com")
	assertNotNull(bidderFromDB)
	def bid = new Bid( amount:2.0, currentAmount: 1.0, bidDate: new Date(), bidder:bidderFromDB )

	//listing
	def listing = new Listing(name:'test', endDate: new Date() + 1, startingBidPrice: 1.0, description: 'test description', seller: seller, 
		winner: null)
	listing.addToBids(bid)

	listing.save(flush:true)
	assertNotNull(listing.getBids())	   
	assertTrue(listing.getBids().size()  == 1)
	
	def bidFromDB =  Bid.findByListing(listing) // will return just one bid now
	def currentAmountDBforBid = bidFromDB.getAmount() // 2.0
	
	def bid3Amount = 3.00
	assertFalse((bid3Amount - currentAmountDBforBid) < 0.50)
	def bid3 = new Bid( amount:bid3Amount, currentAmount: currentAmountDBforBid, bidDate: new Date(), bidder:bidderFromDB )
	listing.addToBids(bid3)
	assertNotNull (listing.save(flush:true))
	assertTrue(listing.getBids().size()  == 2)

	def bid2Amount = 2.20
	assertTrue((bid2Amount - currentAmountDBforBid) < 0.50)
	def bid2 = new Bid( amount:bid2Amount, currentAmount: currentAmountDBforBid, bidDate: new Date(), bidder:bidderFromDB )
	listing.addToBids(bid2)
	
	assertNull (listing.save(flush:true))

    }
}
